package com.qdu.dao;

import com.qdu.entity.ActiComment;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 19645
 */
public class ActiCommentDao {
    
    //根据活动id获取评论列表，返回一个评论列表
    public List<ActiComment> getCommentByActivityId(String movieId) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<ActiComment> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select ACid,Uid,ACcontent,ACtime,ACagree from ActiComment where Aid=?");
            ps.setString(1, movieId);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new ActiComment(
                        rs.getString(1), 
                        rs.getString(2), 
                        rs.getString(3),
                        rs.getString(4),
                        rs.getInt(5)
                )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
    
    //添加评论。根据页面返回的信息，要把评论增加到数据库中
    //ACid用id的值插入，可以实现自增，uid-用户id 用数据库查了再返回过来，
    public int AddComment(String activityId,String Uid,String content){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int row=0;

        try {
            con = DatabaseUtil.getConnection();
            //查找前一个id，+1后就是现在评论的id
            ps = con.prepareStatement("insert ActiComment(ACid,Uid,Aid,ACcontent,ACtime,ACagree) values('2',?,?,?,getdate(),0)");
            ps.setString(1, Uid);
            ps.setString(2, activityId);
            ps.setString(3, content);
            row = ps.executeUpdate();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return row;
    }
    
    //点赞数量增加,传过来评论id
    public int AddAgree(String ACid){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int row=0;

        try {
            con = DatabaseUtil.getConnection();
            //查找前一个id，+1后就是现在评论的id
            ps = con.prepareStatement("update ActiComment set ACagree=ACagree+1 where Acid=?");
            ps.setString(1, ACid);
            row = ps.executeUpdate();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return row;
    }
    
}
